﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.IO;
using System.Configuration;

public partial class houtai_UpFile : System.Web.UI.Page
{

    public static SqlConnection conn()
    {
        string sqlstr = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(sqlstr);
        return con;
    }
    SqlConnection Con = conn();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Upfile_Click(object sender, EventArgs e)
    {
        string strFilePathName = loFile.PostedFile.FileName;
        string strFileName = Path.GetFileName(strFilePathName);
        int FileLength = loFile.PostedFile.ContentLength;
        if (FileLength <= 0)
            return;
        using (Con)
        {

            try
            {//上传文件
                Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
                Stream StreamObject = loFile.PostedFile.InputStream; //建立数据流对像
                //读取图象文件数据，FileByteArray为数据储存体，0为数据指针位置、FileLnegth为数据长度
                StreamObject.Read(FileByteArray, 0, FileLength);
                //建立SQL Server链接
                //string strCon = System.Configuration.ConfigurationManager.AppSettings["DSN"];
                //SqlConnection Con = new SqlConnection(strCon);
                String SqlCmd = "INSERT INTO ImageFile (imagepath,imageContentType, imageDescription, imageSize) VALUES (@Image,@ContentType, @ImageDescription, @ImageSize)";
                SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
                // CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteArray;
                string imgpath = loFile.PostedFile.FileName;// 获取完整路径
                // string imgname=imgpath.Substring(imgpath.IndexOf("//")+1);获取文件后缀名
                string path = "UpFile/UpLoad/File/" + imgpath.ToString();
                CmdObj.Parameters.Add("@image", SqlDbType.NVarChar, 100).Value = path;
                CmdObj.Parameters.Add("@ContentType", SqlDbType.NVarChar, 50).Value = loFile.PostedFile.ContentType; //记录文件类型
                //把其它单表数据记录上传
                CmdObj.Parameters.Add("@ImageDescription", SqlDbType.NVarChar, 200).Value = txtDescription.Value;
                //记录文件长度，读取时使用
                CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = FileLength;
                Con.Open();
                CmdObj.ExecuteNonQuery();
                Con.Close();
                UpFile();
                //跳转页面
                Response.Redirect("UpFile.aspx");
            }

            catch (Exception ex)
            {
                throw ex;
            }

        }

    }
    private void UpFile()
    {
        string FilePath = Server.MapPath("../") + "UpFile/UpLoad/File/";
        HttpFileCollection HFC = Request.Files;
        for (int i = 0; i < HFC.Count; i++)
        {
            HttpPostedFile HF = HFC[i];
            try
            {
                if (HFC[i].ContentLength > 0)
                {

                    HF.SaveAs(FilePath + "//" + System.IO.Path.GetFileName(HF.FileName));
                    this.Page.RegisterStartupScript("sb1", "<script>alert('上传成功！')</script>");
                }
                else
                {
                    this.Page.RegisterStartupScript("sb4", "<script>alert('对不起，请先选择上传的文件路径！')</script>");
                }
            }
            catch
            {
                this.Page.RegisterStartupScript("sb2", "<script>alert('对不起，下载失败！')</script>");
            }
        }
    }
}
